RedisHelper in C#
自己写了一个RedisHelper,现贴出来,希望各位大神能够指正和优化。
using System; using StackExchange.Redis; using System.Configuration; namespace Common.Redis { public static partial class RedisHelper { private static readonly string ConnectString = ConfigurationManager.ConnectionStrings['RedisConnection'].ToString(); private static Lazy<ConnectionMultiplexer> _lazyConnection; private static readonly Object MultiplexerLock = new Object(); private static readonly IDatabase Cache; static RedisHelper() { var conn = CreateManager.Value; Cache = conn.GetDatabase(); //获取实例 } private static Lazy<ConnectionMultiplexer> GetManager(string connectionString = null) { if (string.IsNullOrEmpty(connectionString)) { connectionString = GetDefaultConnectionString(); } return new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(connectionString)); } private static Lazy<ConnectionMultiplexer> CreateManager { get { if (_lazyConnection == null) { lock (MultiplexerLock) { if (_lazyConnection != null) return _lazyConnection; _lazyConnection = GetManager(); return _lazyConnection; } } return _lazyConnection; } } public static string GetDefaultConnectionString() { return ConnectString; } } }使用patial 修饰符,由于Redis中的数据类型操作不少,所以把它们单独分到一个文件。核心的,可优化的主要还是上面的代码。
使用的是StackExchange.Redis Redis 客户端。
连接字符串从配置文件 application web.config读取。配置如下:
<connectionStrings> <add name='RedisConnection' connectionString='10.13.11.101:6379,10.13.18.191:6379,keepAlive=180,allowAdmin=True,$CLIENT=,$CLUSTER=,$CONFIG=,$ECHO=,$INFO=,$PING=' /> </connectionStrings>由于Redis的高速读写的特性,密码是很容易被破解的,所以最好还是部署在内网环境中。
使用 Lazy<ConnectionMultiplexer> 而不是 ConnectionMultiplexer,是可以按需延迟加载。参考文件:
真正的操作方法在下面:
using System; using System.Collections.Generic; using System.Linq; using ServiceStack.Text; using StackExchange.Redis; namespace Common.Redis { public partial class RedisHelper { public const string DefaultOrder = 'desc'; #region Keys public static bool KeyExists(string key) { var bResult = Cache.KeyExists(key); return bResult; } public static bool SetExpire(string key, DateTime datetime) { return Cache.KeyExpire(key, datetime); } public static bool SetExpire(string key, int timeout = 0) { return Cache.KeyExpire(key, DateTime.Now.AddSeconds(timeout)); } public static bool Set<T>(string key, T t, int timeout = 0) { var value = JsonSerializer.SerializeToString(t); bool bResult = Cache.StringSet(key, value); if (timeout > 0) { Cache.KeyExpire(key, DateTime.Now.AddSeconds(timeout)); } return bResult; } public static bool KeyDelete(string key) { return Cache.KeyDelete(key); } public static bool KeyRename(string oldKey, string newKey) { return Cache.KeyRename(oldKey, newKey); } #endregion #region Hashes public static bool IsExist(string hashId, string key) { return Cache.HashExists(hashId, key); } public static bool SetHash<T>(string hashId, string key, T t) { var value = JsonSerializer.SerializeToString(t); return Cache.HashSet(hashId, key, value); } public static bool Remove(string hashId, string key) { return Cache.HashDelete(hashId, key); } public static long StringIncrement(string hashId, string key, long value = 1) { return Cache.HashIncrement(hashId, key, value); } public static T Get<T>(string hashId, string key) { string value = Cache.HashGet(hashId, key); return JsonSerializer.DeserializeFromString<T>(value); } public static long GetHashCount(string hashId) { var length = Cache.HashLength(hashId); return length; } public static string Get(string hashId, string key) { string value = Cache.HashGet(hashId, key).ToString(); return value; } public static List<T> GetAll<T>(string hashId) { var result = new List<T>(); var list = Cache.HashGetAll(hashId).ToList(); if (list.Count > 0) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString<T>(x.Value); result.Add(value); }); } return result; } public static List<string> GetAllFields(string hashId) { var result = new List<string>(); var list = Cache.HashKeys(hashId).ToList(); if (list.Count > 0) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString<string>(x); result.Add(value); }); } return result; } #endregion #region Sorted Sets public static bool SortedSetItemIsExist(string setId, string item) { var value = GetItemScoreFromSortedSet(setId, item); if (value != null) { return true; } return false; } public static bool SortedSetAdd(string setId, string item, double score, int timeout = 0) { return Cache.SortedSetAdd(setId, item, score); } public static List<string> GetSortedSetRangeByRank(string setId, long fromRank, long toRank, string order = DefaultOrder) { var result = new List<string>(); var list = Cache.SortedSetRangeByRank(setId, fromRank, toRank, order == Order.Descending.ToString().ToLower() ? Order.Descending : Order.Ascending).ToList(); if (list.Any()) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString<string>(x); result.Add(value); }); } return result; } public static Dictionary<string, double> GetSortedSetRangeByRankWithScores(string setId, long fromRank, long toRank, string order = DefaultOrder) { var result = new Dictionary<string, double>(); var list = Cache.SortedSetRangeByRankWithScores(setId, fromRank, toRank, order == Order.Descending.ToString().ToLower() ? Order.Descending : Order.Ascending).ToList(); if (list.Any()) { list.ForEach(x => { result.Add(x.Element, x.Score); }); } return result; } public static List<string> GetSortedSetRangeByValue(string setId, long minValue, long maxValue) { var result = new List<string>(); var list = Cache.SortedSetRangeByValue(setId, minValue, maxValue).ToList(); if (list.Any()) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString<string>(x); result.Add(value); }); } return result; } public static long GetSortedSetLength(string setId) { return Cache.SortedSetLength(setId); } public static long GetSortedSetLength(string setId, double minValue, double maxValue) { return Cache.SortedSetLength(setId, minValue, maxValue); } public static long? GetItemRankFromSortedSet(string setId, string item, string order = DefaultOrder) { return Cache.SortedSetRank(setId, item, order == Order.Descending.ToString().ToLower() ? Order.Descending : Order.Ascending); } public static double? GetItemScoreFromSortedSet(string setId, string item) { return Cache.SortedSetScore(setId, item); } public static double SetSortedSetItemIncrement(string setId, string item, double score = 1) { return Cache.SortedSetIncrement(setId, item, score); } public static double SortedSetItemDecrement(string setId, string item, double score = -1) { return Cache.SortedSetDecrement(setId, item, score); } public static bool RemoveItemFromSortedSet(string setId, string item) { return Cache.SortedSetRemove(setId, item); } public static long RemoveByRankFromSortedSet(string setId, long fromRank, long toRank) { return Cache.SortedSetRemoveRangeByRank(setId, fromRank, toRank); } public static long RemoveByScoreFromSortedSet(string setId, double minValue, double maxValue) { return Cache.SortedSetRemoveRangeByScore(setId, minValue, maxValue); } public static long RemoveByLexFromSortedSet(string setId, int minValue, int maxValue) { //TODO: Don't know its meaning //return Cache.SortedSetRemoveRangeByValue(setId, minValue, maxValue); return 0; } #endregion #region Lists public static long AddList<T>(string listId, T t) { var value = JsonSerializer.SerializeToString(t); return Cache.ListLeftPush(listId, value); } public static List<T> GetList<T>(string listId, long start = 0, long stop = -1) { var result = new List<T>(); var list = Cache.ListRange(listId, start, stop).ToList(); if (list.Count > 0) { list.ForEach(x => { var value = JsonSerializer.DeserializeFromString<T>(x); result.Add(value); }); } return result; } #endregion #region Strings public static string Get(string key) { string value = Cache.StringGet(key); return value; } public static T StringGet<T>(string key) { string value = Cache.StringGet(key); return JsonSerializer.DeserializeFromString<T>(value); } public static double StringIncrement(string key, double value) { return Cache.StringIncrement(key, value); } public static long StringAppend(string key, string value) { return Cache.StringAppend(value, value, CommandFlags.None); } #endregion } } ILONEYRedis 有五种常用数据类型,由于我实际中经常使用的是Hash,List,Sorted Set,String,故只我只贴出了上面我测试有限的方法出来,各位可自行补充。
最好自己新建一个文件,这样会比较符合开放-封闭 原则。
最后附上我为了学习Redis最常访问的站点:
Redis 官网:
StackExchange.Redis 的github文档:
https://github.com/StackExchange/StackExchange.Redis
Stackoverflow 站点:
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/nosql/11404.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
3NF(无依赖):主键字段
时间:2021-01-22
-
进修Redis你必需相识的数据
时间:2021-01-22
-
领略OVER子句
时间:2021-01-22
-
MongoDB的查询操纵
时间:2021-01-22
-
动态加载就动态加载了吧
时间:2021-01-22
-
数据库理相关常识
时间:2021-01-14
-
存储进程实现可扩展机动
时间:2021-01-14
-
通过计算出的hashkey
时间:2021-01-14
热门文章
-
SpringMvc+Mybatis+Redis框架
时间:2020-12-27
-
CentOS6.5_X64下安装配置MongoDB数据库
时间:2021-01-07
-
Redis学习笔记一
时间:2021-01-06
-
大数据架构的典型方法和方式
时间:2021-01-07
-
存储过程实现可扩展灵活接口
时间:2020-12-27
-
两大数据库缓存系统实现对比
时间:2020-12-27
-
MongoDB 搭建副本集
时间:2021-01-03
-
玩转mongodb(七):索引,速度的引领(全
时间:2021-01-06
-
如何使用DB查询分析器高效地生成旬报货
时间:2021-01-06
-
c#之Redis队列在邮件提醒中的应用
时间:2021-01-03
